package libra;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;

/**
 * SpringBoot启动入口 
 * @author libra
 */
//标注该类为SpringBoot的主配置类
@SpringBootApplication
//指定包扫描
@ComponentScan(value={"libra","libraServer","libraHystrix"})
// 扫描Servlet组件
@ServletComponentScan(value="libra")
/*
 * @EnableEurekaClient 与 @EnableDiscoveryClient
 * 在功能上是一致的：写在启动类的上，开启服务注册发现功能。
 * @EnableEurekaClient只适用于Eureka作为注册中心，@EnableDiscoveryClient 可以是其他注册中心。
 */
// 开启EurekaClient的注解，连接EurekaServer
@EnableEurekaClient
//@EnableDiscoveryClient
// 开启服务熔断,@EnableHystrix继承了@EnableCricuitBreaker
@EnableHystrix
public class MainStart_ProviderHystrix_10400 {
	public static void main(String[] args) {
		System.out.println("MainStart.main()");
		ConfigurableApplicationContext cac = SpringApplication.run(MainStart_ProviderHystrix_10400.class, args);
	}
	
	/**
	 * 次配置是为了服务监控配置的，与服务容错本身无关，springcloud升级后的坑
	 * ServletRegistrationBean因为springboot的默认路径不是“/hystrix.stream”
	 * 通过手动注册Servlet修改
	 * !!!：dashboard配置成功了，但是jquery版本导致页面报错无法加载，未处理该问题，填写地址http://ip:port/项目名/hystrix.stream
	 * @return
	 */
	@Bean
	public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
		HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
		ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<HystrixMetricsStreamServlet>(streamServlet);
		registrationBean.setLoadOnStartup(1);
		registrationBean.addUrlMappings("/hystrix.stream");
		registrationBean.setName("HystrixMetricsStreamServlet");
		return registrationBean;
	}
}
